/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.sales.hanserver.action.hancadpe;

import br.com.sales.hanserver.action.hancadpe.consts.HanCadPEServerConsts;
import static br.com.sales.hanserver.dao.Dao.closeConn;
import java.sql.PreparedStatement;
import java.util.List;
import java.util.Map;
import static br.com.sales.hanserver.dao.Dao.getConn;
import java.sql.ResultSet;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Karine
 */
public class RefreshPersonDataGrid {

    public List<Map> getItems(Map items) {
        List<Map> resultData = new LinkedList<>();
        try {
            StringBuffer sql;
            sql = new StringBuffer(HanCadPEServerConsts.GET_PERSON);

            if (items != null) {
                if (items.get("NM_PESSOA") != null
                        && !"".equals(items.get("NM_PESSOA").toString())) {
                    sql.append(" and nm_pessoa_fisica like '%").append(items.get("NM_PESSOA").toString()).append("%' ");
                }

                sql.append(" and dt_registro between STR_TO_DATE('").append(items.get("DT_INICIO")).append("','%d/%m/%Y %T')");
                sql.append(" and STR_TO_DATE('").append(items.get("DT_FIM")).append("','%d/%m/%Y %T')");

            }
            sql.append(" order 	by 1,nm_pessoa_fisica");
            PreparedStatement st = getConn().prepareStatement(sql.toString());

            ResultSet rs = st.executeQuery();
            while (rs.next()) {
                Map map = new LinkedHashMap();
                map.put("SEQ", rs.getInt("SEQ"));
                map.put("NM_PESSOA_FISICA", rs.getString("NM_PESSOA_FISICA"));
                map.put("IE_TIPO_PESSOA", rs.getString("IE_TIPO_PESSOA"));
                map.put("DT_NASCIMENTO", rs.getString("DT_NASCIMENTO"));
                map.put("CD_CPF", rs.getString("CD_CPF"));
                map.put("CD_RG", rs.getString("CD_RG"));
                map.put("DS_ENDERECO", rs.getString("DS_ENDERECO"));
                map.put("NR_TELEFONE", rs.getString("NR_TELEFONE"));
                map.put("NR_CELULAR", rs.getString("NR_CELULAR"));
                map.put("NR_SEQ_PAIS", rs.getString("NR_SEQ_PAIS"));
                map.put("NR_SEQ_ESTADO", rs.getString("NR_SEQ_ESTADO"));
                map.put("NR_SEQ_CIDADE", rs.getString("NR_SEQ_CIDADE"));
                map.put("NM_USUARIO", rs.getString("NM_USUARIO"));
                map.put("DS_SENHA", rs.getString("DS_SENHA"));
                map.put("DS_PAIS", rs.getString("DS_PAIS"));
                map.put("DT_REGISTRO", rs.getString("DT_REGISTRO"));
                map.put("DS_ESTADO", rs.getString("DS_ESTADO"));
                map.put("DS_CIDADE", rs.getString("DS_CIDADE"));
                map.put("IE_TIPO_USUARIO", rs.getString("IE_TIPO_USUARIO"));
                resultData.add(map);
            }

            return resultData;

        } catch (Exception ex) {
            Logger.getLogger(GetDataPersonGridAction.class.getName()).log(Level.SEVERE, "Error getDataGrid", ex);
        } finally {
            closeConn();
        }
        return resultData;
    }

}
